package com.tencent.sr.iris.activity.service.activity;

import cn.hutool.core.collection.CollectionUtil;
import com.tencent.sr.iris.activity.dependency.user.UserCoreDependency;
import com.tencent.sr.iris.activity.interfaces.request.acticity.LowerLevelUserCountRequest;
import com.tencent.sr.iris.activity.service.config.ActivityConfig;
import com.tencent.sr.iris.user.core.service.interfaces.enums.UserLevelEnum;
import com.tencent.sr.iris.user.core.service.interfaces.request.user.UserLevelCountRequest;
import com.tencent.sr.iris.user.core.service.interfaces.response.user.UserLevelCountResponse;
import com.tencent.sr.rmall.common.primitive.HttpResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserTaskStatisticsService {

    @Resource
    private ActivityConfig activityConfig;

    @Resource
    private UserCoreDependency userCoreDependency;

    @Resource
    private IrisUserTaskRecordService userTaskRecordService;

    /**
     * 查询天天有礼活动周期内上级用户的下级数量
     *
     * @param request
     * @return
     */
    public HttpResult<List<UserLevelCountResponse>> lowerLevelUserCount(LowerLevelUserCountRequest request) {

        UserLevelCountRequest req = new UserLevelCountRequest();
        req.setUid(request.getUid());
        req.setStartTime(activityConfig.getActRealUserStartTime());
        List<UserLevelCountResponse> userLevelCountResponses = userCoreDependency.countUserLevelByParentUid(req);
        if (CollectionUtil.isNotEmpty(userLevelCountResponses)) {
            userLevelCountResponses.stream().forEach(n -> {
                if (n == null) {
                    return;
                }
                if (UserLevelEnum.FAN.getValue().equals(n.getLevelId())
                        || UserLevelEnum.VIP.getValue().equals(n.getLevelId())) {
                    n.setLevelNums(0L);
                }
            });
        }

        return HttpResult.succ(userLevelCountResponses);
    }
}
